package pers.cj.pdos.controller.other;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Industry;
import pers.cj.pdos.service.ICompanyService;
import pers.cj.pdos.service.IIndustryService;
import pers.cj.pdos.service.IUserService;
import pers.cj.pdos.utils.DateUtils;
import pers.cj.pdos.utils.UuidUtils;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Controller
@RequestMapping("/register")
public class RegisterController extends BaseController {
    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IIndustryService industryService;

    @Autowired
    private IUserService userService;

    @RequestMapping("/page")
    public String page() {
        return "register";
    }

    @RequestMapping("/successful")
    public String successful() {
        return "registerSuccess";
    }

    @RequestMapping("/register")
    public String register(HttpSession session, Model model, String account, String password, String validateCode){

        String sessionValidateCode = (String) session.getAttribute("validateCode");
        if (!sessionValidateCode.equals(validateCode)){
            model.addAttribute("errMsg","验证码错误");
            model.addAttribute("account",account);
            model.addAttribute("password",password);
            return "register";
        }

        Logger.getLogger(getClass().getName()).info(account+"-"+password + "-" + validateCode);
        Company company = new Company();
        company.setId(UuidUtils.getUUID());
        company.setAccount(account);
        company.setCode(account);
        //当前事件
        Date startTime = new Date();
        Date endTime = DateUtils.getDateAfterMonth(startTime,1);
        company.setStartDate(startTime);
        company.setEndDate(endTime);
        company.setAdminName("老板");
        company.setName("XXX企业");
        company.setPhone(account);
        company.setTime(new Date());
        company.setLogo("defaultLogo.png");

        companyService.save(company,password);


        List<Industry> industryList = industryService.listAll();
        model.addAttribute(industryList);
        model.addAttribute("companyId",company.getId());
        return "industry";
    }

    @ResponseBody
    @RequestMapping("/checkAccount")
    public ResponseEntity checkAccount(String account){
        ResponseEntity responseEntity = new ResponseEntity();
        boolean check = companyService.checkAccount(account);
        if (check) {
            responseEntity.setCode(1);
            responseEntity.setMsg("账号已被使用");
            return responseEntity;
        }

        //公司账号不存在还要检查用户名
        check = userService.checkUsername(account);
        if (check) {
            responseEntity.setCode(1);
            responseEntity.setMsg("账号已被使用");
            return responseEntity;
        }


        responseEntity.setCode(2);
        responseEntity.setMsg("账号未被使用");
        return responseEntity;
    }

    @ResponseBody
    @RequestMapping("/industry")
    public ResponseEntity industry(@RequestBody Company company){
        //更新行业编号
        companyService.updateById(company);
        return ResponseEntity.success();
    }



    @RequestMapping("/success")
    public String registerSuccess(){
        return "registerSuccess";
    }
}
